package com.yiren.dbaa.module.device.view.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;

import com.yiren.backstage.communication.thread.RTKConnection;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.SignalSourceConfigBean;
import com.yiren.backstage.config.enums.RoverStationEnum;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.model.ObdCarModel;
import com.yiren.dbaa.util.DensityUtil;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * @author lwb
 * @date :2022/1/13 9:50
 * @description: OBD车型选择弹出框
 */
public class SelectObdModelDialog {

    private Activity mActivity;
    private List<ObdCarModel> obdCarModels;  //数据列表集合

    private Integer obdCarModelCode;
    private ObdCarModel obdCarModel;
    private CommonCallBack callBack;

    public SelectObdModelDialog(Activity mActivity, List<ObdCarModel> obdCarModels, CommonCallBack callBack) {
        this.mActivity = mActivity;
        this.obdCarModels = obdCarModels;
        this.callBack = callBack;
        obdCarModelCode = LoadConfigApi.loadSignalSourceConfig(mActivity).getObdModel();
        if (null == obdCarModel) obdCarModel = new ObdCarModel();
        if (null != obdCarModelCode) {
            setObdModel(obdCarModelCode);
        } else {
            setOdbCarModel();
        }
    }

    /**
     * 有obd车辆枚举数据设置obdCarModel信息
     */
    private void setObdModel(int obdCarModelCode) {
        for (int i = 0; i < obdCarModels.size(); i++) {
            if (obdCarModels.get(i).getNameCode() == obdCarModelCode) {
                obdCarModel.setName(obdCarModels.get(i).getName());
                obdCarModel.setNameCode(obdCarModels.get(i).getNameCode());
            }
        }
    }

    /**
     * 无obd车辆枚举数据设置obdCarModel信息
     */
    private void setOdbCarModel() {
        String strMP = SignalGConstant.carMasterDataMP;
        if (StringUtils.isNotEmpty(strMP)) {
            String[] split = strMP.split(";");
            setObdModel(Double.valueOf(split[1]).intValue());
        }
    }

    public void showDialog() {
        AlertDialog obdCarDialog = new AlertDialog.Builder(mActivity).create();
        obdCarDialog.setCanceledOnTouchOutside(false);
        obdCarDialog.show();
        Window window = obdCarDialog.getWindow();
        window.setContentView(R.layout.dialog_car_select);
        ListView lvStatus = (ListView) window.findViewById(R.id.lv_status);
        Button btnSure = (Button) window.findViewById(R.id.btn_sure);
        StatusAdapter adapter = new StatusAdapter(mActivity);
        lvStatus.setAdapter(adapter);

        btnSure.setOnClickListener(v -> {
            obdCarDialog.dismiss();
            if (null != obdCarModel) {
                SignalSourceConfigBean signalSourceConfig = LoadConfigApi.loadSignalSourceConfig(mActivity);
                if (signalSourceConfig.getRoverStationType() == RoverStationEnum.NO_CARD_ROVER_STATION)
                    RTKConnection.useCommand = true;
                callBack.success(obdCarModel.getNameCode());
            } else {
                MessageBox.show(mActivity, "请先选择车辆型号");
            }
        });
        obdCarDialog.setOnCancelListener(dialog -> LoadDialog.dismiss(mActivity));
        // 自动拉伸
        WindowManager.LayoutParams params = obdCarDialog.getWindow().getAttributes();
        params.width = (DensityUtil.getScreenWidth(mActivity) * 6) / 10;// 修改宽度
        params.height = (DensityUtil.getScreenHeight(mActivity) * 5) / 10;// 修改高度
        obdCarDialog.getWindow().setAttributes(params);
    }

    /***
     * OBD车型列表适配器
     * @author jsb
     */
    class StatusAdapter extends BaseAdapter {

        public StatusAdapter(Context context) {
            super();
        }

        @Override
        public int getCount() {
            return obdCarModels.size();
        }

        @Override
        public Object getItem(int position) {
            return obdCarModels.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, android.view.ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (null == convertView) {
                convertView = mActivity.getLayoutInflater().inflate(R.layout.item_popup_data, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            if (null != obdCarModel) {
                if (obdCarModels.get(position).getName().equals(obdCarModel.getName())) {
                    viewHolder.ctvStatus.setChecked(true);
                } else {
                    viewHolder.ctvStatus.setChecked(false);
                }
            }
            viewHolder.ctvStatus.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    ((CheckedTextView) v).toggle();
                    obdCarModel = obdCarModels.get(position);
                    notifyDataSetChanged();
                }
            });
            viewHolder.ctvStatus.setText(obdCarModels.get(position).getName());
            return convertView;
        }

        class ViewHolder {
            CheckedTextView ctvStatus;

            public ViewHolder(View convertView) {
                ctvStatus = (CheckedTextView) convertView.findViewById(R.id.ctv_status);
            }
        }
    }

}
